/*
 * Amazon Software Tecnologia
 * http://www.amazon-software.net
 */
package tv.util;

import javax.swing.DefaultListModel;
import javax.swing.JEditorPane;
import javax.swing.JList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * DOCUMENT ME!
 *
 * @author $Author$
 * @version $Revision$
 */
public class Utilitario
{
   private static StringBuffer paneContent = null;

   /**
    * DOCUMENT ME!
    *
    * @param valor
    * @param lista
    *
    * @return
    */
   public static DefaultListModel adicionarList(String valor, JList lista)
   {
      DefaultListModel model = new DefaultListModel();
      List<String> listaModel = new ArrayList<String>(0);

      if (lista.getModel().getSize() > 0)
      {
         for (int i = 0; i < lista.getModel().getSize(); i++)
         {
            listaModel.add(lista.getModel().getElementAt(i).toString());
         }
      }
      else
      {
         listaModel.add(valor);
      }

      Collections.sort(listaModel);

      for (int i = 0; i < listaModel.size(); i++)
      {
         String value = listaModel.get(i);
         model.addElement(value);
      }

      return model;
   }

   /**
    * DOCUMENT ME!
    *
    * @param value
    * @param pane
    */
   public static void appendEditorPane(String value, JEditorPane pane)
   {
      StringBuffer buf = new StringBuffer();
      buf.append("<html><head><title></title></head><body>");

      if (paneContent == null)
      {
         paneContent = new StringBuffer();
      }

      paneContent.append(value);
      buf.append(paneContent.toString());
      buf.append("</body></html>");
      pane.setText(buf.toString());

      String caret = paneContent.toString().replace("<p>", "");
      caret = caret.replace("</p>", "");
      pane.setCaretPosition(caret.length());
   }
}
